<%-- 
    Document   : main
    Created on : 5-dec-2009, 16:25:22
    Author     : Simon
--%>

<%@include file="header.jsp" %>

<h1>Welcome ${patient.firstname} ${patient.surname}</h1>

<c:if test="${!empty flash}">
    <div class="flash notice">${flash}</div>
</c:if>

<table id="prescriptionTable" class="data">
    <thead id="prescriptionTableHeader">
        <tr>
            <td>Medication</td>
            <td>Dosage</td>
            <td>Frequency</td>
            <td>Start</td>
            <td>End</td>
            <td>Remarks</td>
        </tr>
    </thead>
    <tbody>
        <c:forEach var="prescription" items="${prescriptions}" varStatus="index">
            <c:set var="task" value="${tasks[index.count - 1]}" scope="request" />
            <!-- Only show prescriptions which have tasks (the other ones are finished, only the GP can review these) -->
            <c:if test="${task != null}">
                <tr>
                    <td>
                        ${prescription.medication.name}
                        <input type="hidden" class="id" value="${prescription.prescriptionId}" />
                    </td>
                    <td>
                        <c:if test="${prescription.dosageIsFixed}">
                            ${prescription.dosageDosage} ${prescription.dosageUnit}
                        </c:if>
                        <c:if test="${!prescription.dosageIsFixed}">
                            <%-- Call web service --%>
                            Variable
                        </c:if>
                    </td>
                    <td>
                        ${prescription.frequency}
                    </td>
                    <td>
                        <fmt:formatDate value="${prescription.startTime}" pattern="d MMM yyyy HH:mm" />
                    </td>
                    <td>
                        <fmt:formatDate value="${prescription.endTime}" pattern="d MMM yyyy HH:mm" />
                    </td>
                    <td>
                        ${prescription.remarks}
                    </td>
                </tr>
                <tr style="margin-bottom: 10px;" class="next_task">
                    <td colspan="6">
                        <div style="margin-left: 20px; border: 0px dashed red; letter-spacing: 1px;">
                            <form method="post" action="${pageContext.request.contextPath}/patient/task/update">
                                <strong style="margin-right: 20px;">Next task</strong>
                                <fmt:formatDate var="today" value="<%= new java.util.Date() %>" pattern="D yy" />
                                <fmt:formatDate var="task_time" value="${task.taskTime}" pattern="D yy" />
                                    <c:choose>
                                        <c:when test="${today == task_time}"><span style="color: red; font-weight: bold;">Today</span></c:when>
                                        <c:otherwise><fmt:formatDate value="${task.taskTime}" pattern="d MMM yyyy" /></c:otherwise>
                                    </c:choose>
                                at 

                                    <fmt:formatDate value="${task.taskTime}" pattern="HH:mm" />

                                <select name="status">
                                    <option value="take">Take medication</option>
                                    <option value="forgotten">Forgot medication</option>
                                    <option value="ignored">Ignore medication</option>
                                </select>

                                <input type="hidden" name="id" value="${task.id}" />

                                <input type="submit" value="Confirm" name="submit" />
                            </form>
                        </div>
                    </td>
                </tr>
            </c:if>
        </c:forEach>
    </tbody>
</table>

<script type="text/javascript">

    $(document).ready(function() {

        $(this).everyTime("2s", function(i) {
            $.getJSON("${pageContext.request.contextPath}/patient/prescription/update", function(data) {
                $.each(data.tasks, function(i, item) {
                    if ($("#prescriptionTable .id[value='"+item.task.prescription.id+"']").size() == 0) {
                        var html = "<tr>";
                        html += "<td>";
                        html += item.task.prescription.medication;
                        html += "<input type='hidden' class='id' value='" + item.task.prescription.id + "' />";
                        html += "</td>";
                        html += "<td>";
                        html += item.task.prescription.dosage;
                        html += "</td>";
                        html += "<td>";
                        html += item.task.prescription.frequency;
                        html += "</td>";
                        html += "<td>";
                        html += item.task.prescription.start_time;
                        html += "</td>";
                        html += "<td>";
                        html += item.task.prescription.end_time;
                        html += "</td>";
                        html += "<td>";
                        html += item.task.prescription.remarks;
                        html += "</td>";
                        html += "</tr>";
                        $("#prescriptionTable").append(html);
                        // Task
                        html = "<tr style=\"margin-bottom: 10px;\">" +
                            "<td colspan='6'>" +
                            "<div style=\"margin-left: 20px; border: 0px dashed red;\">";
                        html += "<form method=\"post\" action=\"${pageContext.request.contextPath}/patient/task/update\">";
                        html += "<strong>Next task</strong> at " + item.task.taskTime;
                        html += "<select name=\"status\">"+
                            "<option value=\"take\">Take medication</option>"+
                            "<option value=\"forgotten\">Forgot medication</option>"+
                            "<option value=\"ignored\">Ignore medication</option>"+
                            "</select>";
                        html += "<input type=\"hidden\" name=\"id\" value=\""+item.task.id+"\" />";
                        html += "<input type=\"submit\" value=\"Confirm\" name=\"submit\" />";
                        html += "</form></div></td></tr>";
                        $("#prescriptionTable").append(html);
                    }
                });
            });
        });
    });

</script>

<%@include file="footer.jsp" %>
